Skip to content

Improve database connection reliability and session management#160

Merged
berggren merged 1 commit intomainfrom
gcs-importer
Mar 17, 2026
Merged

Improve database connection reliability and session management#160
berggren merged 1 commit intomainfrom
gcs-importer

Conversation

@berggren
Copy link
Copy Markdown
Contributor

Summary

Implemented connection health checks and scoped database sessions to the message processing lifecycle to prevent stale connections and resource leaks.

This fixes an issue where the GCS importer would get broken connections to the postgres container.

Technical Details:

  • Connection Resilience: Added pool_pre_ping=True to the SQLAlchemy engine configuration in src/datastores/sql/database.py. This ensures the engine tests the validity of a connection before using it, automatically reconnecting if the database server has closed the socket.
  • Session Lifecycle Management: Refactored the GCP importer to use a context manager for database sessions within the Pub/Sub callback. By moving database.SessionLocal() inside the callback function, the system now creates and closes a fresh session for every message processed.
  • Resource Cleanup: Removed the long-lived database session from the main execution loop. This change eliminates the risk of connection timeouts in long-running subscriber processes and removes the need for manual session closing in the finally block.

@berggren berggren requested a review from hacktobeer March 11, 2026 12:31
Copy link
Copy Markdown
Contributor

@hacktobeer hacktobeer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with one small comment.

@berggren berggren merged commit 89ee620 into main Mar 17, 2026
3 checks passed
@berggren berggren deleted the gcs-importer branch March 17, 2026 11:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants